/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.util;

import java.util.Calendar;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Glauco
 */
public class DataInputVerifier extends InputVerifier{

    Calendar calendar;

    @Override
    public boolean verify(JComponent input) {

        String value = new String();

        JTextComponent tf;

        if (input instanceof JTextComponent){
            tf = (JTextComponent) input;
            value = tf.getText();
        }else
            return true;

        if (value.length() == 0){
            return true;
        }

        value = value.trim();

        // Tira barra, se digitou
        value.replaceAll("/", "");

        // Completa o dia
        if(value.length() == 1){
            value = "0" + value;
        }

        // Coloca o mes
        if(value.length() == 2){
            String month = String.format("%1$tm",
                    getCalendar());
            
            value += month;
        }

        // Formato inválido
        if(value.length() == 3){
            msg(input);
            return false;
        }

        // Coloca o ano
        if(value.length() == 4){
            String year = String.format("%1$tY",
                    getCalendar());

            value += year;
        }

        // formato inválido
        if(value.length() == 5){
            msg(input);
            return false;
        }

        // Completa o ano
        if(value.length() == 6){
            String dayMonth = value.substring(0, 4);
            String year = value.substring(4);

            int intYear = 0;

            try{
                intYear = Integer.parseInt(year);
            }catch(NumberFormatException ex){
                msg(input);
                return false;
            }

            if (intYear > 40){
                year = "19" + year;
            }else{
                year = "20" + year;
            }

            value = dayMonth + year;
        }

        // Formato inválido
        if(value.length() == 7){
            msg(input);
            return false;
        }

        // coloca barra
        if(value.length() == 8)
            value = value.substring(0,2) + "/" +
                    value.substring(2,4) + "/" +
                    value.substring(4);

        tf.setText(value);

        return true;
    }

    private void msg(JComponent input){
        JOptionPane.showMessageDialog(input, "Entrada inválida!");
        if (input instanceof JTextComponent){
            JTextComponent tf = (JTextComponent) input;
            tf.setText("");
        }
    }

    private Calendar getCalendar(){
        if(calendar==null)
            calendar = Calendar.getInstance();
        return calendar;
    }

}
